using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Geekees.App.Cashbook.Lib;
using Geekees.App.Cashbook.Lib.BLL;
using Geekees.App.Cashbook.Lib.Entity;
using Geekees.App.Cashbook.Lib.Exceptions;
using Geekees.App.Cashbook.Lib.Helper;

namespace Geekees.App.Cashbook.Web
{
	/// <summary>
	/// Summary description for Classification.
	/// </summary>
	public class Classification : PageBase
	{
		protected System.Web.UI.WebControls.DropDownList ddlClassification;
		protected System.Web.UI.WebControls.TextBox txtClassification;
		protected System.Web.UI.WebControls.Button btnDelete;
		protected System.Web.UI.WebControls.Button btnSave;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if( !IsPostBack )
				 BindData();
		}

		private void BindData()
		{
			Binder.BindClassification( this.ddlClassification, ListPromptType.Add );
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);

			this.btnDelete.Attributes.Add("onclick","return confirm('Sure?');");
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);
			this.ddlClassification.SelectedIndexChanged+=new EventHandler(ddlClassification_SelectedIndexChanged);
		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if( this.txtClassification.Text.Trim() == string.Empty )
			{
				FlashMessage.SetFlash( "Please give a name befor saving!", FlashMessage.FlashMessageType.Error );
				return;
			}

			if( this.ddlClassification.SelectedIndex == 0 )
			{
				int id = BLClassification.AddClassification( this.txtClassification.Text.Trim() );
				if( id > 0 )
					FlashMessage.SetFlash( "Add Successful!");
			}
			else
			{
				bool r = BLClassification.UpdateClassification( int.Parse( this.ddlClassification.SelectedValue ), this.txtClassification.Text.Trim() ) ;
				if( r )
					FlashMessage.SetFlash( "Update Successful!" );
				else
					FlashMessage.SetFlash( "Update Failed!", FlashMessage.FlashMessageType.Error );


			}
			this.txtClassification.Text = string.Empty;
			this.BindData();
		}

		private void ddlClassification_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.txtClassification.Text = string.Empty;
			if( this.ddlClassification.SelectedIndex != 0 )
				this.txtClassification.Text = this.ddlClassification.SelectedItem.Text;
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if( this.ddlClassification.SelectedIndex == 0 )
			{
				FlashMessage.SetFlash( "Please select one item before deleting!" , FlashMessage.FlashMessageType.Error);
				return;
			}

			try
			{
				BLClassification.DeleteClassification( int.Parse( this.ddlClassification.SelectedValue ) ) ;
			}
			catch(DeleteException de )
			{
				FlashMessage.SetFlash( de.Message, FlashMessage.FlashMessageType.Error );
				return;
			}
			catch(Exception ex)
			{
				FlashMessage.SetFlash( "Delete Failed!" + ex.Message, FlashMessage.FlashMessageType.Error );
				return;
			}
				
			FlashMessage.SetFlash( "Delete Successful!" );
				

			this.txtClassification.Text = string.Empty;
			this.BindData();
		}
	}
}
